package org.chs.ts.batch.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.chs.ts.base.batch.exec.entity.BatchClearBasicTaskExec;
import org.chs.ts.batch.mapper.BatchClearBasicTaskExecMapper;
import org.chs.ts.batch.service.IBatchClearBasicTaskExecService;
import org.chs.ts.batch.util.ConvertToUtil;
import org.chs.ts.batch.util.RedisBuilder;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: batch_clear_basic_task_exec
 * @Author: jeecg-boot
 * @Date: 2024-04-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class BatchClearBasicTaskExecServiceImpl extends ServiceImpl<BatchClearBasicTaskExecMapper, BatchClearBasicTaskExec> implements IBatchClearBasicTaskExecService {

    private final String CLASS_NAME = this.getClass().getSimpleName();

    @Resource
    private BatchClearBasicTaskExecMapper mapper;

    @Resource
    RedisBuilder redisBuilder;

    @Override
    public boolean updateByObject(BatchClearBasicTaskExec obj) {
        boolean updateResult = false;

        // 验证对象 是否需要更新
        long count = this.count(new LambdaQueryWrapper<BatchClearBasicTaskExec>().eq(BatchClearBasicTaskExec::getBatchSysCode, obj.getBatchSysCode()).eq(BatchClearBasicTaskExec::getTaskExecid, obj.getTaskExecid()).eq(BatchClearBasicTaskExec::getExecStatus, obj.getExecStatus()));
        if (count == 0) {
            int i = 0;
            int updateCount = 3;
            do {
                //获取数据更新锁
                String UPDATE_KEY = "UpdateTaskExec";
                if (redisBuilder.checkUpdateRedisLock(UPDATE_KEY + obj.getTaskExecid())) {
                    log.info(CLASS_NAME + " updateByObject method: taskExecid:" + obj.getTaskExecid());
                    updateResult = mapper.updateById(obj) > 0;
                    redisBuilder.unUpdateRedisLock(UPDATE_KEY + obj.getTaskExecid());
                    break;
                }
                redisBuilder.sleepThread();

                i++;
            } while (i < updateCount);

            if (i == updateCount) {
                log.info(CLASS_NAME + " updateByObject method: taskExecid:" + obj.getTaskExecid() + " 更新失败存在数据库redis更新锁");
            }

        } else {
            updateResult = true;
        }


        return updateResult;
    }

    @Override
    public void removeBatch(List<BatchClearBasicTaskExec> tasklist) {
        mapper.deleteBatchIds(tasklist);
        // return mapper.deleteBatchIds(tasklist) > 0;
    }

    @Override
    public boolean saveBatchs(Collection<BatchClearBasicTaskExec> clearBasicTaskExecsList) {

        boolean result = false;
        List<BatchClearBasicTaskExec> rollbackList = new ArrayList<>();
        String ENTITY_NAME = "BatchClearBasicTaskExec";
        for (BatchClearBasicTaskExec item : clearBasicTaskExecsList) {
            result = mapper.insert(item) > 0;

            if (!result) {
                log.info(CLASS_NAME + " -> saveBatchs insert " + ENTITY_NAME + " exception:" + ENTITY_NAME + " -> " + ConvertToUtil.printObjectInfo(item, new ArrayList<>()));
                continue;
            }

            rollbackList.add(item);

        }

        if (!result) {
            log.info(CLASS_NAME + " -> saveBatchs rollabck ");
            for (BatchClearBasicTaskExec item : rollbackList) {
                if (mapper.deleteById(item) <= 0) {
                    log.info(CLASS_NAME + " -> saveBatchs rollback " + ENTITY_NAME + " exception:" + ENTITY_NAME + " -> " + ConvertToUtil.printObjectInfo(item, new ArrayList<>()));
                }
            }
        }

        return result;
    }
}
